package com.yckj.activechicken.business.system.service.impl;

import com.yckj.activechicken.business.system.mapper.SysPostMapper;
import com.yckj.activechicken.business.system.mapper.comp.SysPostMapperComp;
import com.yckj.activechicken.business.system.model.SysPost;
import com.yckj.activechicken.business.system.model.SysPostExample;
import com.yckj.activechicken.business.system.service.ISysPostService;
import com.yckj.activechicken.common.constant.UserConstants;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.framework.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * Title:com.yckj.activechicken.business.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【岗位信息管理业务处理层】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/13 17:36
 */
@Service
public class SysPostServiceImpl implements ISysPostService {
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysPostMapperComp postMapperComp;

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post) {
        return postMapperComp.selectPostList(post);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll() {
        return postMapper.selectByExample(new SysPostExample());
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Integer postId) {
        return postMapper.selectByPrimaryKey(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Integer> selectPostListByUserId(Integer userId) {
        return postMapperComp.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostNameUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getPostId()) ? -1 : post.getPostId();
        SysPostExample sysPostExample = new SysPostExample();
        sysPostExample.createCriteria().andPostNameEqualTo(post.getPostName());
        List<SysPost> postList = postMapper.selectByExample(sysPostExample);

        if (StringUtils.isNotEmpty(postList) && postList.get(0).getPostId().intValue() != postId.intValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostCodeUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getPostId()) ? -1 : post.getPostId();
        SysPostExample sysPostExample = new SysPostExample();
        sysPostExample.createCriteria().andPostCodeEqualTo(post.getPostCode());
        List<SysPost> postList = postMapper.selectByExample(sysPostExample);

        if (StringUtils.isNotEmpty(postList) && postList.get(0).getPostId().intValue() != postId.intValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Integer postId) {
        return postMapperComp.countUserPostById(postId);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Integer postId) {
        return postMapper.deleteByPrimaryKey(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     * @throws Exception 异常
     */
    @Override
    public int deletePostByIds(Integer[] postIds) {
        for (Integer postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new CustomException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return postMapperComp.deletePostByIds(postIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost post) {
        return postMapper.insertSelective(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost post) {
        return postMapper.updateByPrimaryKeySelective(post);
    }
}
